//
//  ViewController.m
//  TestSchool
//
//  Created by wuxiaoquan on 12-11-2.
//  Copyright (c) 2012年 yaoxia. All rights reserved.
//

#import "ViewController.h"
#import "TestListViewController.h"
#import "UserManager.h"
#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize schoolIdTF,passwordTF,navController;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self.passwordTF setSecureTextEntry:YES];
    
    TestListViewController *mainBoradController = [[TestListViewController alloc]initWithNibName:@"TestListViewController" bundle:nil];
    navController = [[UINavigationController alloc]initWithRootViewController:mainBoradController];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.schoolIdTF = nil;
    self.passwordTF = nil;
    self.navController = nil;
    request = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

-(void)userLoginWithId:(NSString *)schoolId password:(NSString *)p
{
    [self showWaitView:nil];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    //NSString * URL_LOGIN = @"http://218.24.94.212/Test/login.jsp";
    NSString * URL_LOGIN = [NSString stringWithFormat:@"%@/Test/login.jsp",delegate.baseUrl];
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:schoolId forKey:@"schoolid"];
    [request setPostValue:p forKey:@"password"];
    
    [request setCompletionBlock:^{
        
        [self hideWaitView];
        
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);//显示收到的信息
        
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"Message"
                                          message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            return ;
        }
        
        User *user = [UserManager getCurrentUser];
        [user clear];
        user.name = [[json objectForKey:@"userinfo"]objectForKey:@"username"];
        user.schoolId = [[json objectForKey:@"userinfo"]objectForKey:@"schoolid"];
        user.userId = [[json objectForKey:@"userinfo"]objectForKey:@"userid"];
        
        if (user.schoolId.length > 0) {
            [self goMainBorad:nil];
        }
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Network error"
                              message:@"Please check your network"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
}
-(IBAction)guestLogin:(id)sender
{
    [self userLoginWithId:@"123456" password:@"123456"];
}

-(IBAction)login:(id)sender
{
    //[self goMainBorad:nil];
    NSString *user = [self.schoolIdTF text];
    NSString *password = [self.passwordTF text];
    if (user.length == 0||password.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning"
                                                       message:@"Please input the complete login information"
                                                      delegate:self
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    [self userLoginWithId:user password:password];
}
-(IBAction)touchBackground:(id)sender
{
    [self.schoolIdTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
}

-(IBAction)goMainBorad:(id)sender
{
    NSLog(@"显示主页！");
    [self presentModalViewController:self.navController animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self touchBackground:nil];
    return TRUE;
}

@end
